<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i> 发布管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>比赛管理</el-breadcrumb-item>
        <el-breadcrumb-item>发布比赛</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="form-box">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="比赛主题" prop="subject">
            <el-input v-model="form.subject"></el-input>
          </el-form-item>

          <el-form-item label="发布人" prop="adminid">
            <el-select v-model="form.adminid" placeholder="请重新选择教练" @change="onClickChange">
              <el-option
                v-for="item in adminData"
                :key="item.adminid"
                :label="item.adminname"
                :value="item.adminid"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="开始日期" prop="startdate">
            <el-date-picker v-model="form.startdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期" prop="stopdate">
            <el-date-picker v-model="form.stopdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
          </el-form-item>
          <el-form-item label="比赛地点" prop="maddress">
            <el-input v-model="form.maddress"></el-input>
          </el-form-item>
          <el-form-item label="比赛简介" prop="mintro">
            <el-input type="textarea" rows="5" v-model="form.mintro"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">发布比赛</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "baseform",
  data: function() {
    return {
      adminData: [],
      form: {
        subject: "",
        maddress: "",
        mintro: "",
        startdate: "",
        stopdate: "",
        adminid: ""
      },
      rules: {
        subject: [
          {
            required: true,
            message: "比赛主题不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5]+$/,
            message: "比赛主题只能为中文",
            trigger: "blur"
          }
        ],
        adminid: [
          {
            required: true,
            message: "请选择发布人",
            trigger: "change"
          }
        ],
        maddress: [
          {
            required: true,
            message: "请填写比赛地址",
            trigger: "blur"
          }
        ],
        mintro: [
          {
            required: true,
            message: "比赛简介不能为空",
            trigger: "blur"
          }
        ],
        startdate: [
          {
            type: "string",
            required: true,
            message: "请选择起止日期",
            trigger: "blur"
          }
        ],
        stopdate: [
          {
            type: "string",
            required: true,
            message: "请选择结束日期",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getadmin();
  },
  methods: {
    onSubmit() {
      let vm = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          vm.$axios.post("/api/Match/addmatch", this.form).then(res => {
            console.log("res :", res.data);
          });
          this.$message.success("添加成功");
          this.$router.push({
            path: "/matchlist"
          });
        } else {
          this.$message.error("添加失败,请填写必填信息");
        }
      });
    },
    getadmin() {
      this.$axios.get("/api/Match/adminlist").then(response => {
        console.log("response :", response.data);
        this.adminData = response.data;
      });
    }
  }
};
</script>
